clear all
set more off, perm
local mydir "`1'"
global Tables 	`mydir'/TS/Tables
global Work   	`mydir'/TS/DataWork
global Raw    	`mydir'/TS/DataRaw
global WRDS  	`mydir'/WRDS
/* ********************************************************************* */
/* ********************************************************************* */
* LOOK AT REVISIONS
/* ********************************************************************* */
use $Work/prgm01, clear

replace fcastE2=fcastE1*(1+LTG/100) if fcastE1>0 & fcastE2==.
replace fcastE3=fcastE2*(1+LTG/100) if fcastE2>0 & fcastE3==.
replace fcastE4=fcastE3*(1+LTG/100) if fcastE3>0 & fcastE4==.
replace fcastE5=fcastE4*(1+LTG/100) if fcastE4>0 & fcastE5==.

tsset datem
forvalues x=1(1)5 {
	gen  eg`x'=ln(fcastE`x'/E)
	gen edg`x'=ln(fcastD`x'/E)
	local j=12*`x'
	gen error`x'=ln(ActualE`x')-ln(fcastE`x')
}
* ***************************************
* CG Variables
gen cg11=ln(fcastE1)-ln(L12.fcastE2) 
gen cg12=ln(fcastE1)-ln(L24.fcastE3) 
gen cg13=ln(fcastE1)-ln(L36.fcastE4) 
gen cg14=ln(fcastE1)-ln(L48.fcastE5) 

gen cg21=ln(fcastE2)-ln(L12.fcastE3)
gen cg22=ln(fcastE2)-ln(L24.fcastE4)
gen cg23=ln(fcastE2)-ln(L36.fcastE5)

gen cg31=ln(fcastE3)-ln(L12.fcastE4)
gen cg32=ln(fcastE3)-ln(L24.fcastE5)

gen cg41=ln(fcastE4)-ln(L12.fcastE5)
* ***************************************
scalar rho=0.96
scalar dis=1/1.08

gen pv=	1.000 * (ln(fcastE1/E)) 					+ ////
	rho^1 * ln(fcastE2/fcastE1)					+ ///
	rho^2 * ln(fcastE3/fcastE2)					+ ///
	rho^3 * ln(fcastE4/fcastE3) 					+ ///
	rho^4 * ln(fcastE5/fcastE4)

gen er= 1.000 * (ln(E/L12.fcastE1)) 					+ ///
	rho^1 * (ln(fcastE1/E)      -ln(L12.fcastE2/L12.fcastE1))	+ ///
	rho^2 * (ln(fcastE2/fcastE1)-ln(L12.fcastE3/L12.fcastE2))	+ ///
	rho^3 * (ln(fcastE3/fcastE2)-ln(L12.fcastE4/L12.fcastE3))	+ ///
	rho^4 * (ln(fcastE4/fcastE3)-ln(L12.fcastE5/L12.fcastE4))	
* *********************************************
gen ep1=(fcastE1-E)/P 
gen ep2=dis * (fcastE2-fcastE1) + dis^2 * (fcastE3-fcastE2) + dis^3 * (fcastE4-fcastE3) + dis^4 * (fcastE5-fcastE4)
replace ep2 = ep2 / P
gen ep=ep1+ep2

gen ep22=dis * ln(fcastE2/fcastE1) * fcastE1/P + dis^2*ln(fcastE3/fcastE2)*fcastE2/P + dis^3*ln(fcastE4/fcastE3)*fcastE3/P+dis^4*ln(fcastE5/fcastE4)*fcastE4/P

pwcorr ep ep1 ep2 
* ***************************************************
save $Work/tmp, replace
* ***************************************************
* REPLACE RETURNS IN THE ORIGINAL DATASET WITH RETURNS FROM CRSP
import sas using $WRDS/CRSP/msp500.sas7bdat, clear

rename caldt date
format date %td

gen datem=mofd(date)
format datem %tm

tsset datem

gen lnret=ln(1+sprtrn)

* compound forward returns (data is monthly)
gen lnret1=0
forvalues x=1(1)12 {
	replace lnret1=lnret1+F`x'.lnret
}
gen lnret2=F12.lnret1+lnret1
gen lnret3=F24.lnret1+lnret2
gen lnret4=F36.lnret1+lnret3
gen lnret5=F48.lnret1+lnret4

* compound lagged returns (data is monthly)
gen lagret1=0
forvalues x=0(1)11 {
	replace lagret1=lagret1+L`x'.lnret
}
gen lagret2=L12.lagret1+lagret1
gen lagret3=L24.lagret1+lagret2
gen lagret4=L36.lagret1+lagret3
gen lagret5=L48.lagret1+lagret4

drop lnret
keep date datem lnret* lagret* sprtrn
merge 1:1 datem using $Work/tmp
* ***************************************************
drop if _merge==1
drop _merge
save $Work/prgm02, replace
* ***************************************************
!rm $Work/tmp.dta -f
